/**
 * Created by Tomkk on 2016/4/30.
 */
angular.module('app.controller',[])
    //登录
    .controller('LoginCtrl',function($state,$scope,SystemService,LoginService){
        $scope.user  = {
            username:'',
            password:'',
            role:''
        };
        $scope.currentPage = "登录";
        $scope.login = function(){
            if($scope.role == ''){

            }
            var promise = LoginService.login($scope.user);
            promise.then(function(data){
                if(data.code == 0){
                    //SystemService.setLogin(true);
                    $state.go('listInfo',{},{reload: true});
                }else{
                    alert("账号或密码错误");
                }
            })
        }
        $scope.goRegister = function(){
            event.preventDefault();//取消点击的默认动作
            $state.go('register',{},{reload: true});
        }
    })
    //注册
    .controller('registerCtrl',function($state,$scope,RegisterService){
        $scope.user = {headimage:""};
        $scope.repeatPassword = "";
        $scope.goLogin = function(){
            event.preventDefault();//取消点击的默认动作
            $state.go('login',{},{reload: true});
        }
        $scope.regiser = function(){
            var promise = RegisterService.register($scope.user);
            promise.then(function(data){
                alert('注册成功');
                $state.go('login',{},{reload: true});
            })
        }
    })
    //信息首页
    .controller('listInfoCtrl',function($scope,$state,ListInfoService,SystemService){
        //判断是否已经登陆
        if(!window.sessionStorage.isLogin){
            $state.go('login');
        }

        //登出
        $scope.logout = function(){
            window.sessionStorage.isLogin = false;
        }

        if(!window.sessionStorage.isLogin){
            $state.go('login');
        }
        var promise = ListInfoService.getInfo();
        promise.then(function(data){
            $scope.info =data;
        })
        $scope.currentPage = "信息列表";
        $scope.isHome = true;
        $scope.keyWords = "";
        $scope.edit = function(index){
            ListInfoService.setCurrentStudent(index);
            $state.go('editUser');
        }
        $scope.goHome = function(){
            event.preventDefault();//取消点击的默认动作
            $state.go('listInfo');
        }
        $scope.goAdd = function(){
            event.preventDefault();//取消点击的默认动作
            $state.go('addUser',{},{reload:true});
        }
        $scope.show = function(index){
            $scope.showStudent = ListInfoService.getShowStudent(index);
        }
        $scope.delete = function(index){
            var res=confirm("确认删除?");
            if(res == false){
                return;
            }
            $scope.info[index].user_isuse = false;
            var promise = ListInfoService.setUnUse(index);
            promise.then(function(data){
                if(data.code == 0){
                    alert(data.returnValue);
                }
            })
        }
    })
    //新增校友
    .controller('addUserCtrl',function($state,$scope,Upload,$timeout,SystemService,RegisterService,ListInfoService){
        //判断是否已经登陆
        if(!window.sessionStorage.isLogin){
            $state.go('login');
        }
        //由于上传插件自身的问题，目前只能采用刷新的方式解决。
        if(window.sessionStorage.isFirst == "true"){
           window.sessionStorage.isFirst = false;
            window.location.reload();
        }

        //登出
        $scope.logout = function(){
            window.sessionStorage.isLogin = false;
        }

        $scope.currentPage = "添加校友";
        $scope.isAdd = true;
        $scope.goHome = function(){
            event.preventDefault();//取消点击的默认动作
            $state.go('listInfo',{},{reload: true});
        }
        $scope.goAdd = function(){
            event.preventDefault();//取消点击的默认动作
            $state.go('addUser',{},{reload:true});
        }
        $scope.user = {
            headimage:"http://localhost/final/pro/img/guest.png"
        };
        $scope.addUser = function(){
            var promise = RegisterService.register($scope.user);
            promise.then(function(data){
                console.log(data);
                ListInfoService.setIsChange();
                alert('添加成功');
            })
        }
        $scope.uploadFiles = function(file, errFiles) {
            $scope.f = file;
            $scope.errFile = errFiles && errFiles[0];
            if($scope.errFile){
                alert("错误原因:     "+ $scope.errFile.$error + '\n'+"最大限制:     " + $scope.errFile.$errorParam);
                return;
            }
            if (file) {
                file.upload = Upload.upload({
                    url: SystemService.getHostIp()+'upload.php',
                    data: {file: file}
                });

                file.upload.then(function (response) {
                    $timeout(function () {
                        if(response.data.code == 0)
                        $scope.user.headimage = response.data.returnValue;
                        alert("上传成功!");
                    });
                }, function (response) {
                    if (response.code > 0)
                        $scope.errorMsg = response.status + ': ' + response.data;
                }, function (evt) {
                    file.progress = Math.min(100, parseInt(100.0 *
                        evt.loaded / evt.total));
                });
            }
        }
    })
    .controller('editUserCtrl',function($scope,$state,ListInfoService,EditUserService,Upload,$timeout,SystemService){
        //判断是否已经登陆
        if(!window.sessionStorage.isLogin){
            $state.go('login');
        }

        //登出
        $scope.logout = function(){
            window.sessionStorage.isLogin = false;
        }

        if(!window.sessionStorage.isLogin){
            $state.go('login');
        }
        //由于上传插件自身的问题，目前只能采用刷新的方式解决。
        if(window.sessionStorage.isFirst == "true"){
            window.sessionStorage.isFirst = false;
            window.location.reload();
        }

        $scope.user = ListInfoService.getCurrentStudent();
        $scope.currentPage = "编辑校友";
        $scope.uploadFiles = function(file, errFiles) {
            $scope.f = file;
            $scope.errFile = errFiles && errFiles[0];
            if($scope.errFile){
                alert("错误原因:     "+ $scope.errFile.$error + '\n'+"最大限制:     " + $scope.errFile.$errorParam);
                return;
            }
            if (file) {
                file.upload = Upload.upload({
                    url: SystemService.getHostIp()+'upload.php',
                    data: {file: file}
                });

                file.upload.then(function (response) {
                    $timeout(function () {
                        if(response.data.code == 0)
                            $scope.user.image = response.data.returnValue;
                        alert("上传成功!");
                    });
                }, function (response) {
                    if (response.code > 0)
                        $scope.errorMsg = response.status + ': ' + response.data;
                }, function (evt) {
                    file.progress = Math.min(100, parseInt(100.0 *
                        evt.loaded / evt.total));
                });
            }
        }
        $scope.updateUser = function(){
            var promise = EditUserService.editUser($scope.user);
            promise.then(function(data){
                alert("修改成功");
                $state.go('listInfo',{},{reload: true});
            });
        }
    })